package com.haixue.android.academy.fragment;

import android.academy.haixue.com.haixue.R;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.haixue.android.academy.MyApplication;
import com.haixue.android.academy.adapter.GoodsAdapter;
import com.haixue.android.academy.domain.GoodsWapper;
import com.haixue.android.academy.utils.Url;

import org.apache.http.Header;

import butterknife.InjectView;
import cn.woblog.android.http.Http;
import cn.woblog.android.http.JsonHandler;
import cn.woblog.android.http.RequestParams;

/**
 * Created by ren on 2015/6/4 0004.
 */
public class GoodsFragment extends  BaseLoadingFragment {

    @InjectView(R.id.elv)
    ExpandableListView elv;
    private GoodsAdapter goodsAdapter;

    @Override
    protected View getLayoutView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_goods,null);
    }

    @Override
    protected void initViews() {
        super.initViews();
        elv.setGroupIndicator(null);
    }

    @Override
    protected void initDatas() {
        super.initDatas();
        goodsAdapter = new GoodsAdapter(getActivity());
        elv.setAdapter(goodsAdapter);

        getData(sp.getSelectCategoryId());
    }

    private void getData(int selectCategoryId) {
        showLoading();
        RequestParams datas = myParams.getDefaultRquestParams();
        datas.put("uid", MyApplication.getUser().getUid()+"");
        datas.put("sk", MyApplication.getUser().getSk());
        datas.put("categoryIds",selectCategoryId);
        Http.get(Url.getURL(Url.GET_GOODS_URL), myParams.genertorReuqestParams(datas), new JsonHandler<GoodsWapper>() {
            @Override
            protected void finish() {
                super.finish();
                hiddLoading();
            }

            @Override
            protected void fail(int statusCode, Header[] headers, String responseString, Throwable throwable) {
                log.debug("get goods faild " + throwable.getMessage());
            }

            @Override
            protected void success(int statusCode, Header[] headers, GoodsWapper data) {
                if (data != null && data.getData() != null) {
                    goodsAdapter.setData(data.getData().get(0).getGoods());
                    log.debug("get goods success " + data.getData().get(0).getGoods().size());
                } else {
                    log.debug("get goods faild");
                }
            }
        });
    }
}
